/**
* @author Meng Zhang
**/

#include "stdafx.h"
#include "BufferBase.h"
#include "BufferController.h"

void BufferBase::AttachListener( BufferListener *listener )
{
	if (listener!=NULL) {
		listeners.insert(listener);
	}
}

void BufferBase::DettachListener( BufferListener *listener )
{
	if (listener!=NULL) {
		listeners.erase(listener);
	}
}

void BufferBase::Update( BufferListener *listener /*= NULL*/ )
{
	std::set<BufferListener*>::iterator iter;
	if (listener==NULL)
		for (iter = listeners.begin() ; iter != listeners.end() ; ++iter) {
			(*iter)->UpdateFromBuffer();
		}
	else {
		iter = listeners.find(listener);
		if (iter!=listeners.end())
			(*iter)->UpdateFromBuffer();
	}

}
BufferBase::~BufferBase() {
	std::set<BufferListener*>::iterator iter;
	for (iter = listeners.begin() ; iter != listeners.end() ; ) {
		BufferListener* listener = *(iter++);
		//listener->DestroyWindow();
		listener->ReAttachBuffer(NULL);
	}
	listeners.clear();
}

BufferBase::BufferBase() {
}

//bool BufferBase::IsTextBuffer() const {
//	return false;
//}